﻿
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;

using Gola.VN.Scb.Web.Common.Entities;

namespace Gola.VN.Scb.Web.Dal.Utilities
{
    class DataTableHelper
    {
        public static IEnumerable<ProviderEntity> GetProviderEntities(DataTable datas)
        {
            var result = new List<ProviderEntity>();

            if (datas != null && datas.Rows != null)
            {
                foreach (DataRow row in datas.Rows)
                {
                    result.Add(new ProviderEntity
                    {
                        Id = Utility.GetInt(row[0]),
                        Name = Utility.GetString(row[1]),
                        Address = Utility.GetString(row[2]),
                        DisplayName = Utility.GetString(row[3]),
                        Logo = Utility.GetString(row[4]),
                        Email = Utility.GetString(row[5]),
                        PhoneNumber = Utility.GetString(row[6]),
                        ExpressionId = Utility.GetInt(row[7])

                    });
                }
            }

            return result;
        }

        public static IEnumerable<CategoryEntity> GetCategoryEntities(DataTable datas)
        {
            var result = new List<CategoryEntity>();

            if (datas != null && datas.Rows != null)
            {
                foreach (DataRow row in datas.Rows)
                {
                    result.Add(new CategoryEntity
                    {
                        Id = Utility.GetInt(row[0]),
                        Name = Utility.GetString(row[1]),
                        Note = Utility.GetString(row[2]),
                        ParentId = Utility.GetInt(row[3])
                    });
                }
            }

            return result;
        }

        public static IEnumerable<ProductEntity> GetProductEntities(DataTable datas)
        {
            var result = new List<ProductEntity>();

            if (datas != null && datas.Rows != null)
            {
                foreach (DataRow row in datas.Rows)
                {
                    result.Add(new ProductEntity
                    {
                        Id = Utility.GetInt(row[0]),
                        ProviderId = Utility.GetInt(row[1]),
                        CategoryId = Utility.GetInt(row[2]),
                        Title = Utility.GetString(row[4]),
                        Description = Utility.GetString(row[5]),
                        ImageUrl = Utility.GetString(row[6]),
                        Link = Utility.GetString(row[7]),
                        LastPrice = Utility.GetDouble(row[8]),
                        Price = Utility.GetDouble(row[9]),
                        Currency = Utility.GetString(row[10]),
                        Promotion = Utility.GetString(row[11]),
                        CreateDate = Utility.GetDateTime(row[12]),
                        ProductId = Utility.GetString(row[13]),
                        ProdiverName = Utility.GetString(row[14]),
                        ProdiverAddress = Utility.GetString(row[15]),
                        CategoryName = Utility.GetString(row[16]),
                        ViewCount = Utility.GetInt(row[17])
                    });
                }
            }

            return result;
        }

        public static IEnumerable<LocationEntity> GetLocationEntities(DataTable datas)
        {
            var result = new List<LocationEntity>();

            if (datas != null && datas.Rows != null)
            {
                foreach (DataRow row in datas.Rows)
                {
                    var location = new LocationEntity
                    {
                        Latitude = Utility.GetString(row[0]),
                        Longitude = Utility.GetString(row[1]),
                        Address = Utility.GetString(row[2]),
                        Title = Utility.GetString(row[3]),
                        ProviderId = Utility.GetInt(row[4]),
                    };
                    result.Add(location);
                }
            }

            return result;
        }

        public static List<SearchHistoryEntity> GetSearchHistoryEntities(DataTable datas)
        {
            var result = new List<SearchHistoryEntity>();

            if (datas != null && datas.Rows != null)
            {
                foreach (DataRow row in datas.Rows)
                {
                    result.Add(new SearchHistoryEntity
                    {
                        Text = Utility.GetString(row[0]),
                        Link = Utility.GetString(row[1]),
                        Count = Utility.GetInt(row[2]),
                        LastDate = Utility.GetDateTime(row[3])
                    });
                }
            }

            return result;
        }
    }
}
